/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.dragdrop;

import com.google.inject.Inject;
import listo.client.ContextManager;
import listo.client.Convert;
import listo.client.actions.Actions;
import listo.client.model.Tasks;
import listo.client.model.operations.DeleteFoldersOp;
import listo.client.model.operations.Operation;
import listo.client.viewlists.FolderView;
import listo.client.viewlists.View;
import listo.utils.logging.Log;

import javax.swing.*;

public class TrashViewListTransferHandler extends BaseTransferHandler {

    private final Actions actions;

    @Inject
    public TrashViewListTransferHandler(ContextManager contextManager, Log log, Actions actions) {
        super(contextManager, log);
        this.actions = actions;
    }

    //********************************************************************
    //*                             EXPORT
    //********************************************************************

    public int getSourceActions(JComponent c) {
        return NONE;
    }

    //********************************************************************
    //*                             IMPORT
    //********************************************************************

    @SuppressWarnings({"RedundantIfStatement"})
    public boolean canImport(TransferSupport support) {
        if (!support.isDrop() || support.getUserDropAction() != MOVE) return false;

        JList.DropLocation location = (JList.DropLocation) support.getDropLocation();
        if (location.getIndex() != 0) return false;

        if (support.isDataFlavorSupported(TaskSelection.TASK_FLAVOR)) {
            Tasks dragTasks = (Tasks) getSelection(support, TaskSelection.TASK_FLAVOR);
            return !dragTasks.isEmpty() && dragTasks.allActive();
        }
        if (support.isDataFlavorSupported(FolderViewSelection.VIEW_FLAVOR)) {
            View[] dragViews = (View[]) getSelection(support, FolderViewSelection.VIEW_FLAVOR);
            return dragViews.length > 0;
        }

        return false;
    }

    protected Operation createDropOperation(TransferSupport support) {
        if (support.isDataFlavorSupported(TaskSelection.TASK_FLAVOR)) {
            if (actions.detachCurrentFoldersAction.isEnabled()) {
                actions.detachCurrentFoldersAction.actionPerformed(null);
            } else {
                actions.deleteTasksAction.actionPerformed(null);
            }
            return null;
        }
        if (support.isDataFlavorSupported(FolderViewSelection.VIEW_FLAVOR)) {
            FolderView[] dragViews = (FolderView[]) getSelection(support, FolderViewSelection.VIEW_FLAVOR);
            DeleteFoldersOp op = new DeleteFoldersOp();
            op.setFolders(Convert.toFolderIds(dragViews));
            return op;
        }
        return null;
    }

}